package com.huilian.iotbox.mqtt.server.base.service.impl;

import com.huilian.iotbox.mqtt.server.base.dao.BaudDao;
import com.huilian.iotbox.mqtt.server.base.service.BaudService;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.po.Baud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @author 谢洛涛
 * @date 2021/8/31 11:11
 */
@Service
public class BaudServiceImpl implements BaudService {
    @Resource
    private BaudDao baudDao;

    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Override
    public Baud findOneBaudByCode(Integer baudCode) {

        Baud baud = (Baud) redisCacheTemplate.opsForValue().get(RedisKey.BAUD_INFO + baudCode);
        if (StringUtils.isEmpty(baud)) {
            Baud baudCondition = new Baud();
            baudCondition.setCode(baudCode);
            baud = this.findOneBaudByCondition(baudCondition);

            if (!StringUtils.isEmpty(baud)) {
                redisCacheTemplate.opsForValue().set(RedisKey.BAUD_INFO + baudCode, baud, 1, TimeUnit.DAYS);
            }
        }

        return baud;
    }

    @Override
    public Baud findOneBaudByCondition(Baud baudCondition) {
        return baudDao.findOneBaudByCondition(baudCondition);
    }

}
